package com.tjetc.view.manage;

import com.tjetc.domain.Doctor;
import com.tjetc.domain.Newborn;
import com.tjetc.domain.Vaccination;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.DoctorService;
import com.tjetc.service.NewBornService;
import com.tjetc.service.VaccinationService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class DoctorView {
    private VaccinationService vaccinationService=ClientProxy.getProxyInstance(VaccinationService.class);
    private DoctorService doctorService= ClientProxy.getProxyInstance(DoctorService.class);
    private NewBornService newBornService=ClientProxy.getProxyInstance(NewBornService.class);
    private Scanner sc=new Scanner(System.in);
    public void start() {
        while(true){
            try {
                System.out.println("1.查询所有医护人员信息");
                System.out.println("2.添加医护人员信息");
                System.out.println("3.修改医护人员信息");
                System.out.println("4.删除医护人员信息");
                System.out.println("5.按照姓名查询医护人员");
                System.out.println("6.查找管理某新生儿的医护人员");
                System.out.println("7.查找为某新生儿接种某疫苗的医护人员");
                System.out.println("8.展示为某新生儿接种疫苗的所有医护人员");
                System.out.println("9.设置医护人员给新生儿接种疫苗");
                System.out.println("0.返回上一级");
                int i= sc.nextInt();
                switch (i){
                    case 1:
                        list();
                        break;
                    case 2:
                        add();
                        break;
                    case 3:
                        update();
                        break;
                    case 4:
                        delete();
                        break;
                    case 5:
                        getDoctorByName();
                        break;
                    case 6:
                        getDoctorByNewBornId();
                        break;
                    case 7:
                        getDoctorByVaccine();
                        break;
                    case 8:
                        getDoctorByNewBornIdAll();
                        break;
                    case 9:
                        setdoctor();
                        break;
                    case 0:
                        return;
                    default:
                        System.out.println("输入有误，请重新输入");
                        break;
                }
            }catch (Exception e){
                System.out.println("输入错误,请重新输入");
                sc.nextLine();
            }
        }
    }

    private void setdoctor() {
        System.out.println("请输入新生儿的编号");
        int id=sc.nextInt();
        Newborn newborn =newBornService.findBId(id);
        if (newborn==null){
            System.out.println("该新生儿不存在");
        }else {
            System.out.println("请输入接种的疫苗的编号");
            int yimiaoId=sc.nextInt();
            System.out.println("请输入负责接种的医生的编号");
            String doctorId=sc.next();
            Vaccination vaccination = new Vaccination(id,yimiaoId,doctorId);
            Result result = vaccinationService.set(vaccination);
            System.out.println(result.getMsg());
        }
    }
    private void getDoctorByNewBornIdAll() {//查找为某新生儿接种疫苗的医护人员
        System.out.println("请输入新生儿的id");
        int id = sc.nextInt();
        List<Doctor> doctorList = doctorService.getDoctorByNewBornIdAll(id);
        for (Doctor doctor : doctorList) {
            System.out.println(doctor);
        }
    }

    private void getDoctorByVaccine() {//查找为某新生儿接种某疫苗的医护人员
        System.out.println("请输入新生儿的id");
        int NewBornId = sc.nextInt();
        System.out.println("请输入疫苗的id");
        int VaccineId = sc.nextInt();
        String  doctorByVaccine = doctorService.getDoctorByVaccine(NewBornId, VaccineId);
        System.out.println(doctorByVaccine);
    }

    private void getDoctorByNewBornId() {//查找管理某新生儿的医护人员
        System.out.println("请输入新生儿id");
        int id = sc.nextInt();
        Doctor doctor = doctorService.getDoctorByNewBornId(id);
        System.out.println(doctor);
    }

    private void getDoctorByName() {//查找某医护人员
        System.out.println("请输入医护人员的姓名");
        String name = sc.next();
        Doctor doctor = doctorService.getDoctorByName(name);
        System.out.println(doctor);
    }

    private void delete() {
        System.out.println("请输入医护人员id");
        int id =sc.nextInt();
        Result result = doctorService.delete(id);
        System.out.println(result.getMsg());
    }

    private void update() {
        list();
        System.out.println("请输入医护人员id");
        int id = sc.nextInt();
        System.out.println("请输入医护人员的姓名");
        String name= sc.next();
        System.out.println("请输入医护人员的密码");
        String password = sc.next();
        Doctor doctor = new Doctor(id,name,password);
        Result result = doctorService.update(doctor);
        System.out.println(result.getMsg());
    }

    private void add() {
        System.out.println("请输入请输入医护人员的姓名");
        String username = sc.next();
        System.out.println("请输入请输入医护人员的密码");
        String password = sc.next();
        Doctor doctor = new Doctor();
        doctor.setUsername(username);
        doctor.setPassword(password);
        Result result = doctorService.add(doctor);
        System.out.println(result.getMsg());
    }

    private void list() {
        List<Doctor> doctorList = doctorService.getDoctorList();
        for (Doctor doctor : doctorList) {
            System.out.println(doctor);
        }
    }
}
